package com.example.utils.http;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;

public class HuToolHttpDemo {
    public static void main(String[] args) {

        new ArrayList<>().stream().filter(n-> n.getClass().toString().isEmpty());

        System.out.println("---------------------Post登录--------------------------");
        String url = "http://1.117.242.65:8090/prod-api/login";
        String loginInfo = "{\"username\":\"admin\",\"password\":\"admin123\",\"code\":\"9\",\"uuid\":\"647b0795cdd444338ff75c2b41a403f8\"}";
        String result = login(url, loginInfo);
        System.out.println("Post登录成功,返回:" + result);

        System.out.println("---------------------获取TOKEN--------------------------");
        String token = (JSON.parseObject(result).get("token")).toString();
        System.out.println(token);

        System.out.println("---------------------带TOKEN请求--------------------------");
        url = "http://1.117.242.65:8090/prod-api/system/user/list?pageNum=1&pageSize=10";
        String test1 = HttpRequest.get(url)
                .header(Header.AUTHORIZATION, "Bearer " + token)
                .timeout(2000)//超时，毫秒
                .execute().body();
        System.out.println(test1);
    }

    public static String login(String url, String loginInfo) {
        return HttpRequest.post(url)
                .header(Header.ACCEPT, "application/json, text/plain, */*")
                .header(Header.ACCEPT_ENCODING, "application/json, text/plain, */*")
                .header(Header.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9")
                .header(Header.CONNECTION, "keep-alive")
                .header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")
                .header(Header.HOST, "127.0.0.1")
                .header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
                .body(loginInfo)//表单内容
                .timeout(2000)//超时，毫秒
                .execute().body();
    }
}
